package com.randi.common.exception;

import com.randi.common.enums.ResultCode;
import com.randi.common.utils.StringUtils;

public class BaseException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    /**
     * 错误码
     */
    private final Integer code;

    /**
     * 错误提示
     */
    private final String message;

    /**
     * 错误码对应的参数
     */
    private final Object[] args;

    /**
     * 错误消息
     */
    private final String defaultMessage;

    public BaseException(Integer code, String message, Object[] args, String defaultMessage) {
        this.code = code;
        this.message = message;
        this.args = args;
        this.defaultMessage = defaultMessage;
    }

    public BaseException(Integer code, String message) {
        this(code, message, null, null);
    }

    public BaseException(Integer code, String message, Object[] args) {
        this(code, message, args, null);
    }

    public BaseException(ResultCode resultCode) {
        this(resultCode.status(), resultCode.message(), null, null);
    }

    public BaseException(ResultCode resultCode, Object[] args) {
        this(resultCode.status(), resultCode.message(), args, null);
    }

    public BaseException(ResultCode resultCode, Object[] args, String defaultMessage) {
        this(resultCode.status(), resultCode.message(), args, defaultMessage);
    }

    @Override
    public String getMessage() {
        return StringUtils.format(message, args);
    }

    public Integer getCode() {
        return this.code;
    }

    public Object[] getArgs() {
        return this.args;
    }

    public String getDefaultMessage() {
        return defaultMessage;
    }

}
